Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Presentation.Controllers
Imports KanBan.Pgd.Presentation.Views
Imports KanBan.Pgd.Domain.ValueObjects
Imports KanBan.Pgd.Infrastructure.Repositories
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Services

Public Class ShowUser

    Private reloadPermissions As Boolean = False

    Private app As New PgdApp()

#Region "IShowUserView"

    Private _id As Guid
    Public Property Id() As Guid
        Get
            Return _id
        End Get
        Set(ByVal value As Guid)
            _id = value
        End Set
    End Property

    Public Property NameOfUser() As String
        Get
            Return lblNameText.Text
        End Get
        Set(ByVal value As String)
            lblNameText.Text = value
        End Set
    End Property

    Public Property UserName() As String
        Get
            Return lblUserNameText.Text
        End Get
        Set(ByVal value As String)
            lblUserNameText.Text = value
        End Set
    End Property

    Public ReadOnly Property IndividualPermissionsList() As CheckedListBox
        Get
            Return clbIndividualPermssions
        End Get
    End Property

    Public ReadOnly Property TabContainer() As TabControl
        Get
            Return Me.MainTabContainer
        End Get
    End Property

    Public Property FormTitle() As String
        Get
            Return txtFormTitle.Text
        End Get
        Set(ByVal value As String)
            txtFormTitle.Text = value
        End Set
    End Property

#End Region

    Public Sub New(ByVal userId As Guid)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        'Info Tab
        Me.LoadInfoTab(userId)

        'Permissions Tab
        Me.LoadPermissionsTab()

    End Sub

    Private Sub LoadInfoTab(ByVal userId As Guid)

        Dim userRepository As New UserRepository()
        Dim user As User = userRepository.GetById(userId)

        Me.FormTitle = user.Name
        Me.Id = user.Id
        Me.NameOfUser = user.Name
        Me.UserName = user.UserName

        Dim permissions = app.Permissions.GetAll()
        Dim userPermissions As New List(Of PermissionInfo)

        For Each p As Permission In permissions
            If user.HasPermission(p.Id) Then
                userPermissions.Add(New PermissionInfo(p.Id, p.Name, True))
            Else
                userPermissions.Add(New PermissionInfo(p.Id, p.Name, False))
            End If
        Next

        Me.AddIndividualPermissions(userPermissions)

    End Sub

    Private Sub AddIndividualPermissions(ByVal permissions As List(Of PermissionInfo))

        Me.IndividualPermissionsList.DisplayMember = "Name"

        For Each p As PermissionInfo In permissions
            Me.IndividualPermissionsList.Items.Add(p, p.Checked)
        Next

    End Sub

    Public Sub AddIndividualPermission(ByVal permissionId As Guid)

        Dim userRepository As New UserRepository()
        Dim userId As Guid = Me.Id

        Dim user As User = userRepository.GetById(userId)

        Dim permission = app.Permissions.GetById(permissionId)

        user.AddPermission(permission)

        userRepository.SaveOrUpdate(user)

    End Sub

    Private Sub LoadPermissionsTab()

        Dim form As New ShowPermissions(Me.Id)
        form.TopLevel = False
        form.Dock = DockStyle.Fill

        UIHelper.LoadExistingTabPage(Me.TabContainer, form, UserTabs.Permissions)

        form.Show()

    End Sub


    Private Sub clbIndividualPermssions_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles clbIndividualPermssions.ItemCheck

        reloadPermissions = True

        If clbIndividualPermssions.SelectedItem IsNot Nothing Then
            Dim permissionId As Guid = CType(clbIndividualPermssions.SelectedItem, PermissionInfo).Id

            If e.NewValue = CheckState.Checked Then
                Me.AddIndividualPermission(permissionId)
            Else
                Me.RemoveIndividualPermission(permissionId)
            End If
        End If

    End Sub

    Public Sub RemoveIndividualPermission(ByVal permissionId As Guid)


        Dim userRepository As New UserRepository()
        Dim userId As Guid = Me.Id

        Dim user As User = userRepository.GetById(userId)

        Dim permission = app.Permissions.GetById(permissionId)

        user.RemovePermission(permission.Id)

        userRepository.SaveOrUpdate(user)

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub MainTabContainer_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MainTabContainer.SelectedIndexChanged

        If MainTabContainer.SelectedTab.Name = UserTabs.Permissions Then
            If reloadPermissions Then
                Me.ReloadPermissionsTab()
                reloadPermissions = False
            End If
        End If

    End Sub

    Public Sub ReloadPermissionsTab()

        Me.LoadPermissionsTab()

    End Sub

End Class